import sys
from confs import IBV_SINGLE, MI_MODEL_PATH, word_rank

def load_mutual_infomation_model():
    pc = {}
    for line in open(MI_MODEL_PATH):
        c, v = line.split()
        pc[c] = float.fromhex(v)
    return pc

def ibv_word(mi_model, w):
    if len(w) == 1:
        return IBV_SINGLE
    else:
        return min(mi_model[w[i: i + 2]] for i in range(len(w) - 1))

def main():
    '''
    NOTE: no reducer for this job
    input: ebv_iteration_<T>.txt where T is number of iterations
    output: word_rank.txt
    (word, '<'? '>'? lbv rbv degl degr llinkees.. rlinkees..) -> (word, word_rank(word))
    '''
    mi_model = load_mutual_infomation_model()
    for line in sys.stdin:
        line = line.strip()
        tokens = line.split()
        word = tokens[0]
        for token in tokens[1: ]:
            if ':' in token:
                key, val = token.split(':')
                if key == 'lbv':
                    lbv = float.fromhex(val)
                elif key == 'rbv':
                    rbv = float.fromhex(val)
        ibv = ibv_word(mi_model, word)
        wr = word_rank(lbv, rbv, ibv)
        print '%s\tlbv:%g rbv:%g ibv:%g wordrank:%g' % (word, lbv, rbv, ibv, wr)

if __name__ == '__main__':
    main()

